package com.xxx.userservice.controller;

import com.xxx.util.JwtUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "/user")
@RefreshScope   // 开启nacos配置自动刷新
public class LoginController {

    @Value("${configInfo.redisTimeOut}")
    private long redisTimeOut;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * http://localhost:9999/user/login?username=guweiqiang&password=12345
     */
    @GetMapping("/login")
    public String login(String username, String password) {
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return "";
        }

        // jwt生成token
        String token = JwtUtil.getToken(username);
        // 保存到redis
        stringRedisTemplate.opsForValue().set(token, token, redisTimeOut, TimeUnit.SECONDS);
        return token;
    }


}
